<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/20
 * Time: 9:45
 */

namespace app\alliance\controller;


use app\service\MemberCode;
use app\service\MemberService;
use Redis\Redis;
use think\App;
use think\Controller;
use think\Db;
use think\facade\Request;

class Common extends Controller
{
    public $memberInfo;
    public $code;
    public $level;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        header('Access-Control-Allow-Origin:*');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        header('Access-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers,token');  //响应头 请按照自己需求添加。
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
            exit;
        }
        $token = Request::header('token');
        # token 验证
        $tokenRes = $this->tokenVerification($token);
        if ($tokenRes['status'] == false) {
            throw new \BaseException(['msg' => $tokenRes['msg'], 'status' => false, 'debug' => false]);
        }
        # 日志
        MemberService::logLogin($this->code, $this->memberInfo['name']);
    }

    /**
     * @param $token
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content token解析验证
     */
    function tokenVerification($token)
    {
        # 判断是否为空
        if (!empty($token)) {
            # token解析
            $code = MemberCode::authCode($token);
            if (!empty($code)) {
                $redis = new Redis();
                # 根据解析的code获取用户信息
                $memberInfo = $redis->hGetJson('memberInfo', $code);
                if (empty($memberInfo)) {
                    # 缓存中信息为空 , 查询数据库
                    $memberInfo = Db::table('member m')
                        ->field('m.*,mp.*,mc.*,ml.title,(select  COALESCE(sum(earnings_price),0) from member_business mb where mb.member_code = m.code and mb.status != -1 and place_status=1) combat_power')
                        ->leftJoin('member_property mp', 'mp.member_code=m.code and mp.is_fake=1')
                        ->leftJoin('member_collection mc', 'mc.member_code=m.code')
                        ->leftJoin('member_level ml', 'ml.level=m.level')
                        ->where(array('code' => $code))->find();
                    if (!empty($memberInfo)) {
                        # 头像地址
                        $memberInfo['head_portrait'] = imgurl($memberInfo['head_portrait']);
                        # 微信收款码
                        $memberInfo['wx_img'] = imgurl($memberInfo['wx_img']);
                        # 支付宝收款码
                        $memberInfo['ali_img'] = imgurl($memberInfo['ali_img']);
                        # 银联收款码
                        $memberInfo['union_img'] = imgurl($memberInfo['union_img']);
                        $memberInfo['gold'] = priceFormat($memberInfo['gold']);
                        $memberInfo['materials'] = priceFormat($memberInfo['materials']);
                        $memberInfo['integral'] = priceFormat($memberInfo['integral']);
                        $memberInfo['combat_power'] = priceFormat($memberInfo['combat_power']);
                        # 存入缓存
                        $redis->hSetJson('memberInfo', $code, $memberInfo);
                    } else {
                        # 为查到用户信息
                        return array('status' => false, 'msg' => 'token解析失败');
                    }
                }
                # 赋值
                $this->memberInfo = $memberInfo;
                $this->code = strtoupper($code);
                $this->level = $this->memberInfo['level'];
                return array('status' => true, 'msg' => 'token解析成功');
            } else {
                return array('status' => false, 'msg' => 'token解析错误');
            }
        } else {
            return array('status' => false, 'msg' => 'token为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 判断是否满足激活条件(满足条件直接激活)
     */
    function isActivationSatisfied()
    {
        # 调用接口  查询 是否满足激活条件
        if ($this->memberInfo['status'] == 5) {
            return array('status' => false, 'msg' => '已经激活');
        } else {
            # 查消费积分是否满足
            $logIntegral = Db::table('log_integral')->where(array('member_code' => $this->code, 'type_branch' => 7))->sum('total_money');
            if ($logIntegral >= 388) {
                # 满足条件 ,可以激活
                MemberService::receiveBusiness($this->code);
                return array('status' => true, 'msg' => '满足激活条件');
            } else {
                # 不满足
                return array('status' => false, 'msg' => '不满足激活条件');
            }
        }
    }

}